﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using SpotiKat.Spotify.Core;
using SpotiKat.Spotify.Core;
using SpotiKat.Spotify.Manager;

namespace SpotiKat.Spotify.Manager
{
    public class LookupManager
    {
        public static LookupManager Instance { get { return new LookupManager(); } }

        public ArtistLookup LookupArtist(string href)
        {
            return LookupArtist(href, ArtistLookupExtras.None);
        }

        public ArtistLookup LookupArtist(string href, ArtistLookupExtras extras)
        {
            ArtistLookup result = new ArtistLookup();
            result.Href = href;

            SpotifyManager spotifyManager = SpotifyManager.Instance;
            
            string url = Settings.Default.LookupServiceUrl;
            string ex = "";

            if (extras != ArtistLookupExtras.None)
            {
                ex = extras.ToString().ToLower();
            }

            url = string.Format(url, href, ex);

            XDocument response = spotifyManager.GetResponse(url);

            XElement xelement = response.Root;

            if (xelement != null)
            {
                result.Artist = spotifyManager.GetArtistWithAlbums(xelement);
                if (result.Artist != null)
                {
                    result.Artist.Href = href;
                    result.Success = true;
                }
            }

            return result;
        }

        public AlbumLookup LookupAlbum(string href)
        {
            return LookupAlbum(href, AlbumLookupExtras.None);
        }

        public AlbumLookup LookupAlbum(string href, AlbumLookupExtras extras)
        {
            AlbumLookup result = new AlbumLookup();
            result.Href = href;

            SpotifyManager spotifyManager = SpotifyManager.Instance;

            string url = Settings.Default.LookupServiceUrl;
            string ex = "";

            if (extras != AlbumLookupExtras.None)
            {
                ex = extras.ToString().ToLower();
            }

            url = string.Format(url, href, ex);

            XDocument response = spotifyManager.GetResponse(url);

            XElement xelement = response.Root;

            if (xelement != null)
            {
                result.Album = spotifyManager.GetAlbumWithTracks(xelement);
                if (result.Album != null)
                {
                    result.Album.Href = href;
                    result.Success = true;
                }
            }

            return result;
        }

        public TrackLookup LookupTrack(string href)
        {
            TrackLookup result = new TrackLookup();
            result.Href = href;

            SpotifyManager spotifyManager = SpotifyManager.Instance;

            string url = Settings.Default.LookupServiceUrl;

            url = string.Format(url, href, "");

            XDocument response = spotifyManager.GetResponse(url);

            XElement xelement = response.Root;

            if (xelement != null)
            {
                result.Track = spotifyManager.GetTrack(xelement);
                if (result.Track != null)
                {
                    result.Track.Href = href;
                    result.Success = true;
                }
            }

            return result;
        }
    }
}
